package conceptmaniax.hotelapp.activities;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TableLayout;
import conceptmaniax.hotelapp.adapters.Simple;
import conceptmaniax.hotelapp.adapters.SimpleListAdapter;
import conceptmaniax.hotelapp.config.Constants;
import conceptmaniax.hotelapp.enums.Language;
import conceptmaniax.hotelapp.util.GeneralUtils;
import conceptmaniax.hotelapp.util.database.DataHelper;

public class SetButtonActivity  {
    private static final SetButtonActivity INSTANCE = new SetButtonActivity();
    private Context context;
	private  DataHelper dataHelper = null;
	private Language language;
    private Button bt1;
    private Button bt2;
    private Button bt3;
    private TableLayout callout;
    private ListView list;
    static int pos = 0;
    private boolean rec;
    
    private SetButtonActivity() {
    	
    }
    
    public static SetButtonActivity getInstance() {
        return INSTANCE;
    }
    
    public void setRecognition (boolean b) {
    	this.rec = b;
    }
    
    public void setButtons(final Context context, final Button bt1, final Button bt2, final Button bt3, final Button bt4, final TableLayout callout, final ListView list, final View screen, final Button requests) {
		this.context = context.getApplicationContext();
		this.dataHelper = new DataHelper(context, Constants.DATABASE_NAME);
	    this.language  = GeneralUtils.getLanguage(context);

		switch (this.language) {
		case ENGLISH:
			bt1.setText(Constants.HOTEL_BUTTON_EN);
			bt2.setText(Constants.SERVICES_BUTTON_EN);
			bt3.setText(Constants.TRAVEL_BUTTON_EN);
			bt4.setText(Constants.BOOKING_BUTTON_EN);
			break;
		case GREEK:
			bt1.setText(Constants.HOTEL_BUTTON_GR);
			bt2.setText(Constants.SERVICES_BUTTON_GR);
			bt3.setText(Constants.TRAVEL_BUTTON_GR);
			bt4.setText(Constants.BOOKING_BUTTON_GR);
			break;
		}
    	this.bt1 = bt1;
    	this.bt2 = bt2;
    	this.bt3 = bt3;
    	this.callout = callout;
    	this.list = list;
    	
		bt1.setOnClickListener(new View.OnClickListener() {
			public void onClick(final View view) {			
				 if (!bt1.isSelected()) {
					bt2.setSelected(false);
					bt3.setSelected(false);
					displayMenu();
					if (rec)
						fillMenu("room");
					else
						fillMenu("home");
					bt1.setSelected(true);
				} else {
					removeMenu();
					bt1.setSelected(false);
				}
			}
		});
		
		bt2.setOnClickListener(new View.OnClickListener() {
			public void onClick(final View view) {
			 if (!bt2.isSelected()) {
				 bt1.setSelected(false);
				 bt3.setSelected(false);
					displayMenu();
					fillMenu("service");
					bt2.setSelected(true);
				} else {
					removeMenu();
					bt2.setSelected(false);
				}
			}
		});
				
		bt3.setOnClickListener(new View.OnClickListener() {
			public void onClick(final View view) {
				 if (!bt3.isSelected()) {
					bt1.setSelected(false);
					bt2.setSelected(false);
					displayMenu();
					fillMenu("guide");
					bt3.setSelected(true);
				} else {
					removeMenu();
					bt3.setSelected(false);
				}
			}
		});
		
		bt4.setOnClickListener(new View.OnClickListener() {
			public void onClick(final View view) {
				 if (bt1.isSelected() || bt2.isSelected() || bt3.isSelected()) {
					 removeMenu();
					 bt1.setSelected(false);
					 bt2.setSelected(false);
					 bt3.setSelected(false);
				 }
				final Intent bookingIntent = new Intent(context, WebViewActivity.class);
				bookingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(bookingIntent);
			}
		});
		
		if (requests != null)
 		requests.setOnClickListener(new View.OnClickListener() {
 			@Override
			public void onClick(View v) {
				final Intent requestsIntent = new Intent(context, RequestListActivity.class);
				requestsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(requestsIntent);			
				}
		});
 		
		screen.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (callout.getVisibility() == View.VISIBLE) {
				    Animation a = AnimationUtils.loadAnimation(context, R.anim.disappear);
				    a.reset();
					callout.clearAnimation();
					callout.startAnimation(a);
					callout.setVisibility(View.INVISIBLE);
					bt1.setSelected(false);
					bt2.setSelected(false);
					bt3.setSelected(false);
					return true;
				} else			
					return false;
			}
		});
	}		
	
	private void displayMenu() {
	    Animation a = AnimationUtils.loadAnimation(context, R.anim.appear);
	    a.reset();
		callout.clearAnimation();
		callout.startAnimation(a);
		callout.setVisibility(View.VISIBLE);	
	}
	
	private void removeMenu() {
	    Animation a = AnimationUtils.loadAnimation(context, R.anim.disappear);
	    a.reset();
		callout.clearAnimation();
		callout.startAnimation(a);
		callout.setVisibility(View.INVISIBLE);	
	}
		
	private void fillMenu(final String type) {
		String menuQuery = null;
		switch (this.language) {
		case ENGLISH:
		menuQuery = "SELECT m._id,"
				  + " m.name_en," 
				  + " m.type,"
			      + " m.activity" 
				  + " FROM menu m"
				  + " WHERE m.type='"+type+"'";
			break;
		case GREEK:
			menuQuery = "SELECT m._id,"
					  + " m.name_gr," 
					  + " m.type,"
				      + " m.activity" 
					  + " FROM menu m"
					  + " WHERE m.type='"+type+"'";
			break;
		}
		
	Cursor cursor =	dataHelper.getData(menuQuery);
	
	final SimpleListAdapter adapter = new SimpleListAdapter(context);
	if (rec && type.equals("room")) {
		adapter.addItem(new Simple(0,null,"Accommodation"));
	}
	for (int i = 0; i < cursor.getCount(); i++) {
		cursor.moveToNext();
		int id = cursor.getInt(0); // _id
		String name = cursor.getString(1); // name
		String action = cursor.getString(3); // action
		adapter.addItem(new Simple(id, action, name));
	}
	cursor.close();

	if ( dataHelper != null ) dataHelper.close();
	
	list.setClickable(true);
	list.setAdapter(adapter);
	list.setOnItemClickListener(new OnItemClickListener() {
		@Override
		public void onItemClick(final AdapterView<?> adapterView, final View view, final int position, final long index) {
			Simple item = new Simple();
			item = (Simple) adapter.getItem(position);
			String action = item.getCode();
			String name = item.getName();
			
			removeMenu();
			bt1.setSelected(false);
			bt2.setSelected(false);
			bt3.setSelected(false);
			
			if (type.equals(Constants.HOTEL_MENU_NAME)) {
				if (action.equals(Constants.ABOUT_MENU_NAME) || action.equals(Constants.BAR_MENU_NAME) || action.equals(Constants.EVENTS_NAME) 
					|| action.equals(Constants.FACILITIES_NAME) || action.equals(Constants.SPA_MENU_NAME) || action.equals(Constants.CONTACT_NAME)) {
					final Intent MenuDetailsIntent = new Intent(view.getContext(), DetailItemActivity.class);
					MenuDetailsIntent.putExtra("action", action);
					if (action.equals(Constants.CONTACT_NAME))
						MenuDetailsIntent.putExtra("contact", "1");
					MenuDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(MenuDetailsIntent);
				} else if (action.equals(Constants.LOCATION_MENU_NAME)) {
					final Intent MapIntent = new Intent(view.getContext(), DetailMapActivity.class);
					MapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(MapIntent);
				} else if (action.equals(Constants.ACCOMMODATION_NAME)) {
					final Intent AccomodationIntent = new Intent(view.getContext(), BasicItemActivity.class);
					AccomodationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(AccomodationIntent);
				} else if (action.equals(Constants.LANGUAGE_MENU_NAME)) {
					final Intent LanguageIntent = new Intent(view.getContext(), LanguageSelectionActivity.class);
					LanguageIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(LanguageIntent);
				 }
				} else if (type.equals(Constants.ROOM_MENU_NAME)) {
					final Intent MenuDetailsIntent = new Intent(view.getContext(), DetailItemActivity.class);
					MenuDetailsIntent.putExtra("action", action);
					MenuDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(MenuDetailsIntent);
				} else if (type.equals(Constants.SERVICE_MENU_NAME)) {
					if (action.equals("checkin") || action.equals("checkout")) {
						final Intent FormIntent = new Intent(view.getContext(), CheckinActivity.class);
						FormIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						FormIntent.putExtra("menu_id",Integer.toString(item.getId()));
						FormIntent.putExtra("name", name);
						FormIntent.putExtra("action", action);
						context.startActivity(FormIntent);
					} else {
						final Intent FormIntent = new Intent(view.getContext(), FormActivity.class);
						FormIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						FormIntent.putExtra("menu_id",Integer.toString(item.getId()));
						FormIntent.putExtra("name", name);
						FormIntent.putExtra("action", action);
						context.startActivity(FormIntent);
					}
				} else if (type.equals(Constants.GUIDE_MENU_NAME)) {
					if (action.equals("weather")) {
						final Intent FormIntent = new Intent(view.getContext(), WeatherActivity.class);
						FormIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(FormIntent);
					}
				}
		  	}
	  });
	}
    public void autoMenu() {
    	if (rec) {
			fillMenu("room");
    		displayMenu();
			bt1.setSelected(true);
    	}
    }
}